

clear all
set mem 2000m
set maxvar 30000

unzipfile thetas15_alpha3_beta001_all_both2015.dta.zip
use thetas15_alpha3_beta001_all_both2015.dta
erase thetas15_alpha3_beta001_all_both2015.dta

set more off

local j 15
local conflict_type 2

local cheat .

gen year = int(date/10000)

sort countryid year

by countryid year: egen total_tokens = sum(tokens)
	

	
local s = (`j'-1) 



gen obs = 1

collapse theta* (sum)  tokens obs, by(country countryid year)



local real_alpha = 50/15
			

					

forval t = 0 / `s' {


	quietly gen words`t' = theta`t' * tokens
	quietly gen ste_theta`t' = (words`t'+`real_alpha')/(tokens+`j'*`real_alpha')
	drop words`t'
 
	drop  theta`t'
	
} 


		

sort countryid year

merge 1:1 countryid year using complete_main_new



quietly replace tokens = tokens/1000

sort year
by year: egen total_tokens = sum(tokens)
tsset countryid year
replace tokens = tokens/total_tokens
replace tokens = L1.tokens if tokens==.&L1.tokens!=.
quietly gen tokens_sq = tokens*tokens
	

tsset countryid year


if `conflict_type' == 2 {

quietly gen conflict = bdbest25

}

if `conflict_type' == 3 {


quietly gen conflict = bdbest1000

}


                        
generate onset=conflict==1&L1.conflict==0
                        
generate over=conflict==1&F1.conflict==0
                        
generate one_before=conflict==0&F1.conflict==1
generate two_before=conflict==0&(F1.conflict==1|F2.conflict==1)


if "`cheat'" == "." {
	replace one_before= 1 if conflict==1&F1.conflict==1
	replace two_before= 1 if conflict==1&(F1.conflict==1|F2.conflict==1)
}

generate sincelast = conflict==0
replace sincelast = L1.sincelast+1 if L1.sincelast>0 & l1.sincelast!=. & conflict==0

generate sincelast_sq = sincelast*sincelast
generate sincelast_cu = sincelast_sq*sincelast


drop if _merge != 3
drop _merge


by countryid: egen total_conflict = sum(conflict)

tsset countryid year


* newer version controlling for other topics

by countryid: egen ever_conflict = max(conflict)

gen conflict_not_onset = conflict
replace conflict_not_onset = 0 if onset == 1



forval x = 1 / 11 {
	gen year_onset`topic'_`x' = .
	gen year_onset_u`topic'_`x' = .
	gen year_onset_d`topic'_`x' = .
}




xtreg ste_theta1 ste_theta0 ste_theta2 ste_theta4-ste_theta14 L4.conflict L3.conflict L2.conflict L1.conflict F4.onset F3.onset F2.onset F1.onset if conflict != 1  , fe


forval x = 1/4 {		
		replace year_onset_`x' = _b[F`x'.onset]
		replace year_onset_u_`x' = _b[F`x'.onset] + 1.96 * _se[F`x'.onset]	
		replace year_onset_d_`x' = _b[F`x'.onset] - 1.96 * _se[F`x'.onset]	
	}
	
	
local x 5
			
forval x = 1/4 {	
		local y = `x' + 5
		replace year_onset_`y' = _b[L`x'.conflict]
		replace year_onset_u_`y' = _b[L`x'.conflict] + 1.96 * _se[L`x'.conflict]	
		replace year_onset_d_`y' = _b[L`x'.conflict] - 1.96 * _se[L`x'.conflict]	
	}	

keep year_onset* 

gen what = 1

keep if _n == 1
reshape long year_onset_ year_onset_u_ year_onset_d_ , i(what) j(years_before)

replace years_before = years_before - 5
replace years_before = -years_before - 5 if years_before < 0

gen ach = 0
sort years_before
	 
drop if years_before < -2
drop if years_before > 4

	 
twoway (scatter year_onset_  years_before ,  mcolor(blue) ) ///
	(rcap year_onset_u_  year_onset_d_ years_before, pstyle(p1) color(black)) ///
	(line ach  years_before,  color(gs6) lcolor(gs6) lpattern(dash) ) , xline(0) text(0.008 0 "Conflict", place(e) color(red)) ///
	legend(order (1 "Regression coefficient" 2 "95% CI")  size(small)) xlabel(-2(1)4) ///
	 plotregion(fcolor(white)) graphregion(fcolor(white)) ytitle("")  xtitle("Years before onset / after conflict") 
	graph export output/fig8.eps , replace 
	 *graph export justice_years_before_after_new_better.eps , replace 

